package cn.hxzy.course.service.impl;

import cn.hxzy.common.domain.Chapter;
import cn.hxzy.common.domain.Section;
import cn.hxzy.common.domain.Video;
import cn.hxzy.common.domain.VideoJd;
import cn.hxzy.course.mapper.ChapterMapper;
import cn.hxzy.course.mapper.SectionMapper;
import cn.hxzy.course.mapper.VideoJdMapper;
import cn.hxzy.course.mapper.VideoMapper;
import cn.hxzy.course.service.VideoService;
import cn.hxzy.common.util.ThreadLocalUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author niuyi
 * @since 2024-06-22
 */
@Service
public class VideoServiceImpl extends ServiceImpl<VideoMapper, Video> implements VideoService {
    @Autowired
    private VideoMapper videoMapper;

    @Autowired
    private ChapterMapper chapterMapper;

    @Autowired
    private VideoJdMapper videoJdMapper;

    @Autowired
    private SectionMapper sectionMapper;

    @Autowired
    private RedisTemplate<String, Object> redisTemplate;


    @Override
    public void saveUserIdToRedis(Long videoId, Integer videoJD) {
        Long userId = ThreadLocalUtil.getUser().getUserId();
        String key = userId + ":" + videoId;
        redisTemplate.opsForValue().set(key, videoJD);
    }

    @Override
    public List<Video> selectVideoList(Integer sectionId) {
        QueryWrapper<Video> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("section_id", sectionId);
        List<Video> videos = videoMapper.selectList(queryWrapper);
        if (videos != null) {
            return videos;
        }
        return null;
    }

    @Override
    public List<Video> selectVideo(String videoName, Long courseId) {
        return videoMapper.selectVideo(videoName, courseId);
    }

    // 根据课程Id视频时长
    @Override
    public Long getVideoTime(Integer courseId) {
        Long courseTime = 0L;
        QueryWrapper<Chapter> queryWrapper = new QueryWrapper();
        queryWrapper.eq("course_id", courseId);
        List<Chapter> chapters = chapterMapper.selectList(queryWrapper);
        for (Chapter chapter : chapters) {
            QueryWrapper<Section> wrapper = new QueryWrapper();
            wrapper.eq("chapter_id", chapter.getChapterId());
            List<Section> sections = sectionMapper.selectList(wrapper);
            for (Section section : sections) {
                QueryWrapper<Video> wrapper1 = new QueryWrapper();
                wrapper1.eq("section_id", section.getSectionId());
                List<Video> videos = videoMapper.selectList(wrapper1);
                for (Video video : videos) {
                    String videoHours = video.getVideoHours();
                    Double d = Double.parseDouble(videoHours);
                    long l = Math.round(d);
                    courseTime += l;
                }
            }
        }
        return courseTime / 3600;
    }

    @Override
    public double selectCount(Long courseId) {
        double num = videoMapper.selectCount(courseId);
        return num;
    }





    @Override
    public int deleteVideo(Integer videoId) {
        int i = videoMapper.deleteById(videoId);
        return i;
    }

    @Override
    public String selectJd(Integer videoId) {
        Long userId = ThreadLocalUtil.getUser().getUserId();
        String videoJdBfb = videoMapper.selectVideoJd(userId, videoId);
        return videoJdBfb;
    }

    @Override
    public String updateJd(Integer videoId) {
        Long userId = ThreadLocalUtil.getUser().getUserId();
        QueryWrapper<VideoJd> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", userId);
        queryWrapper.eq("video_id", videoId);
        VideoJd videoJd = videoJdMapper.selectOne(queryWrapper);
        if (videoJd!=null){
            if (videoJd.getVideoJdBfb().equals("100")){
                return "success";
            }else {
                videoJd.setVideoJdBfb("100");
                videoJdMapper.updateById(videoJd);
            }
        }else{
            videoJd = new VideoJd();
            videoJd.setUserId(userId);
            videoJd.setVideoId(videoId);
            videoJd.setVideoJdBfb("100");
            videoJdMapper.insert(videoJd);
        }
        return "success";
    }

    @Override
    public String selectVideoJd(long userId, Integer videoId) {
        // 根据用户Id和视频Id查询视频进度
        //redis缓存 查询
        String key = userId + ":" + videoId;
        if (redisTemplate.hasKey(key)) {
            Object o = redisTemplate.opsForValue().get(key);
            //输出o的类型
            if (o instanceof String) {
                String s = (String) o;
                return s;
            }

        }
        //线程中获取用户Id
//        Long userId1 = ThreadLocalUtil.getUser().getUserId();
        String string = videoMapper.selectVideoJd(userId, videoId);
        if (string != null) {
            return string;
        }
        return "0";
    }

    @Override
    public String getVideoPath(Integer videoId) {
        return videoMapper.getVideoPath(videoId);
    }

}
